package com.cn.intelligence.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * Created by hackcoder on 15-4-20.
 */
public class HttpNetClient {
	public static String GET(String url) {
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod(url);
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
		try {
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				return "Method failed: " + method.getStatusLine();
			}
			byte[] responseBody = method.getResponseBody();
			return new String(responseBody);

		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
		return null;
	}

	public static String POST(String url, String string) {
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		post.setRequestHeader("content-type", "application/json");
		try {
			RequestEntity se = new StringRequestEntity(string.toString(), "application/json", "UTF-8");
			post.setRequestEntity(se);
			int statusCode = httpClient.executeMethod(post);
			if (statusCode != HttpStatus.SC_OK) {
				return "Method failed: " + post.getStatusLine();
			}
			byte[] responseBody = post.getResponseBody();
			return new String(responseBody);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			post.releaseConnection();
		}
		return null;
	}

	public static String DELETE(String url, Map<String, Object> dataForm,String loginname) {
		HttpClient httpClient = new HttpClient();
		DeleteMethod deleteMethod = new DeleteMethod(url);
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		if (dataForm != null) {
			Set<String> keys = dataForm.keySet();
			for (String key : keys) {
				NameValuePair nameValuePair = new NameValuePair(key, (String) dataForm.get(key));
				data.add(nameValuePair);
			}
		}
		deleteMethod.setQueryString(data.toArray(new NameValuePair[0]));
		try {
			int statusCode = httpClient.executeMethod(deleteMethod);
			if (statusCode != HttpStatus.SC_OK) {
				return "Method failed: " + deleteMethod.getStatusLine();
			}
			byte[] responseBody = deleteMethod.getResponseBody();
			return new String(responseBody);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			deleteMethod.releaseConnection();
		}
		return null;
	}

	public static String PUT(String url, String dataForm,String loginname) {
		HttpClient httpClient = new HttpClient();
		PutMethod putMethod = new PutMethod(url);
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		System.out.println(data.toArray(new NameValuePair[0]));
		putMethod.setQueryString(dataForm);
		try {
			int statusCode = httpClient.executeMethod(putMethod);
			if (statusCode != HttpStatus.SC_OK) {
				return "Method failed: " + putMethod.getStatusLine();
			}
			byte[] responseBody = putMethod.getResponseBody();
			return new String(responseBody);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			putMethod.releaseConnection();
		}
		return null;
	}
	public static String urlPutMethod(String url, String params,String loginname) {
		HttpClient httpClient = new HttpClient();
		PutMethod method = new PutMethod(url);
		method.setRequestHeader("content-type", "application/json");
		try {
			if (params != null && !params.trim().equals("")) {
				RequestEntity requestEntity = new StringRequestEntity(params, "text/xml", "UTF-8");
				method.setRequestEntity(requestEntity);
			}
			method.releaseConnection();
			httpClient.executeMethod(method);
			String responses = method.getResponseBodyAsString();
			return responses;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String urlDeleteMethod(String url,String loginname) {
		HttpClient httpClient = new HttpClient();
		DeleteMethod deleteMethod = new DeleteMethod(url);
		try {
			int statusCode = httpClient.executeMethod(deleteMethod);
			if (statusCode != HttpStatus.SC_OK) {
				return "Method failed: " + deleteMethod.getStatusLine();
			}
			byte[] responseBody = deleteMethod.getResponseBody();
			return new String(responseBody);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			deleteMethod.releaseConnection();
		}
		return null;
	}
	
	public static String urlPostMethod(String url, String params,String loginname) {
		HttpClient httpClient = new HttpClient();
		PostMethod method = new PostMethod(url);
		method.setRequestHeader("content-type", "application/json");
		try {
			if (params != null && !params.trim().equals("")) {
				RequestEntity requestEntity = new StringRequestEntity(params, "text/xml", "UTF-8");
				method.setRequestEntity(requestEntity);
			}
			method.releaseConnection();
			httpClient.executeMethod(method);
			String responses = method.getResponseBodyAsString();
			return responses;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String httpurl  = "https://api.weixin.qq.com/sns/jscode2session";//?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
		Map<String,Object> outputStr = new HashMap<String,Object>();

//		appid	是	小程序唯一标识
		outputStr.put("appid", "APPID");
//		secret	是	小程序的 app secret
		outputStr.put("secret", "SECRET");
//		js_code	是	登录时获取的 code
		outputStr.put("js_code", "code");
//		grant_type	是	填写为 authorization_code
		outputStr.put("grant_type", "authorization_code");
		String a= httpurl+"?"+outputStr.toString();
		//String s = GET("https://api.weixin.qq.com/sns/jscode2session?appid=wxbf146818744d6146&secret=14d26abc620743d9d76e3abe1e03df4b&js_code=071Q6jQ224qstZ09Q4R22rlgQ22Q6jQv&grant_type=authorization_code");
		System.out.println(a);
	}
}